﻿using ReflectionStudio.Core.Events;
using System.Collections.Generic;
using System.IO;
using WPFExcelReport.Helpers;
using System.Data;
namespace WPFExcelReport
{
    public partial class VICC_MULLANG
    { 
        VICC_FUNCTIONS FC;
        public void GetAllText2Translate(string sub)
        {
            if (sub == "")
                sub = app.company; 
            string root = @"D:\ITXRPRT\Reports\" + sub;
            GetFiles(root);
        }
        public void ProcessFiles(string file)
        {

            if (file.ToString() == "")
                return;
            if (!file.EndsWith(".xaml")) return;
            if (file.EndsWith("App.xaml")) return;
            string sourceFile = file;
            string destinationFile = file;
            Dictionary<int, string> lines2replace = new Dictionary<int, string>();
            // Read the appropriate line from the file.
            string lineToWrite = null;
            System.Text.Encoding encUTF8 = System.Text.Encoding.UTF8;
            string[] lines = File.ReadAllLines(sourceFile);

            using (StreamReader reader = new StreamReader(sourceFile))
            {
                for (int i = 1; i <= lines.Length; ++i)
                {
                    int idx = -1, idx2 = -1, idx1 = -1;
                    int startidx = -1;
                    string line2write;
                    string str = reader.ReadLine();
                    idx1 = str.IndexOf("Word="); if (idx1 > 0) continue;
                    idx = str.IndexOf("Content=");
                    if (idx > 0)
                    {
                        idx1 = str.IndexOf("Binding"); if (idx1 > 0) continue;
                        startidx = idx + 9;
                    }
                    else
                    {
                        if (idx == -1)
                            idx = str.IndexOf("Text=");
                        if (idx > 0)
                        {
                            idx1 = str.IndexOf("Binding"); if (idx1 > 0) continue;
                            idx1 = str.IndexOf("TextBox"); if (idx1 > 0) continue;
                            idx1 = str.IndexOf("ComboBox"); if (idx1 > 0) continue;
                            startidx = idx + 6;
                        }
                        else
                        {
                            if (idx == -1)
                                idx = str.IndexOf("Header=");
                            if (idx > 0)
                            {
                                idx1 = str.IndexOf("Binding"); if (idx1 > 0) continue;
                                startidx = idx + 8;
                            }
                            else
                            {
                                if (idx == -1)
                                    idx = str.IndexOf("ToolTip=");
                                if (idx > 0)
                                {
                                    idx1 = str.IndexOf("Binding"); if (idx1 > 0) continue;
                                    startidx = idx + 9;
                                }
                            }
                        }
                    }
                    if (idx == -1) continue;
                    idx2 = str.IndexOf("\"", startidx);
                    string content = str.Substring(startidx, idx2 - startidx);
                    if (content.Trim() == "") continue;
                    lineToWrite = str.Substring(0, startidx) + "{Binding Source={Localization:Translate Word=" + content + "}}" + str.Substring(idx2);
                    lines2replace.Add(i, lineToWrite);
                }
            }

            lines = File.ReadAllLines(destinationFile);
            // Write the new file over the old file.
            using (StreamWriter writer = new StreamWriter(destinationFile))
            {
                for (int currentLine = 1; currentLine <= lines.Length; ++currentLine)
                {
                    if (lines2replace.ContainsKey(currentLine))
                    {
                        lineToWrite = lines2replace[currentLine];
                        writer.WriteLine(lineToWrite);
                    }
                    else
                    {
                        writer.WriteLine(lines[currentLine - 1]);
                    }
                }
            }
            TextFileReader reader1 = null;
            reader1 = new TextFileReader(file, ",");
            string prog = "";
            foreach (var text in reader1)
            {
                int idx = -1, idx2 = -1, idx1 = -1;
                string str = text[0];
                idx = str.IndexOf("x:Class=");
                if (str.IndexOf("T:ViewBase") >= 0)
                    break;
                if (idx > 0)
                {
                    idx = str.IndexOf("WPFExcelReport.");
                    if (idx > 0)
                    {
                        //<local:DocumentBase x:Class="WPFExcelReport.YCAWDPLAN"
                        idx2 = str.Length;
                        prog = str.Substring(idx + 14 + 1, idx2 - idx - 14 - 2);
                        break;
                    }
                }
            }
            if (prog == "") return;
            TBPORG = new DataTable();
            dwData = TBMain.NewRow();
            dwData["MANDANT"] = app.Mandant;
            dwData["MLLOC"] = prog;
            SResult = QMUL02(dwData);
            TBPORG = SResult.Tables[0];
            foreach (var text in reader1)
            {
                int idx = -1, idx2 = -1, idx1 = -1;
                string str = text[0];
                string content = "";

                idx = -1;
                idx = str.IndexOf("Word=");
                if (idx == -1) continue;
                idx2 = str.IndexOf("}}", idx);
                if (idx2 == -1) continue;
                content = str.Substring(idx + 5, idx2 - idx - 5);
                Insert2DB(content, prog);
            }

        }
        DataTable TBPORG = null;
        public void GetFiles(string root)
        {
            List<string> dirs = new List<string>(Directory.EnumerateDirectories(root));
            List<string> files;
            List<string> dirsub;
           
            System.Text.Encoding encUTF8 = System.Text.Encoding.UTF8;
            files = new List<string>(Directory.EnumerateFiles(root));
            foreach (var file in files)
            {
                EventDispatcher.Instance.RaiseStatus("开始处理" + file, StatusEventType.StopProgress);
                ProcessFiles(file);
            }
            foreach (var dir in dirs)
            {
                if(dir.Contains("TVAART\\UI"))
                {

                }
                files = new List<string>(Directory.EnumerateFiles(dir));
                dirsub= new List<string>(Directory.EnumerateDirectories(dir));
                foreach(var dirss in dirsub)
                {
                    GetFiles(dirss);
                }
                foreach (var file in files)
                {                    
                    EventDispatcher.Instance.RaiseStatus("开始处理" + file, StatusEventType.StopProgress);
                    ProcessFiles(file); 
                }
            }
            POSTCHG();
        }
        public void Insert2DB(string str, string loc)
        {

            dwData = TBMain.NewRow();
            dwData["MANDANT"] = "00";
            //LFD = VSDDAT.SD22.GETNEXT("0022", "  ", "MULLANG", 1);
            dwData["ROWNID"] = "0";
            dwData["MLKEY"] = (Helper.ConvertGB2312To8859P1(str).PadRight(120).Substring(0, 40)).Trim();
            dwData["MLLOC"] = loc;
            dwData["SCN"] = (Helper.ConvertGB2312To8859P1(str).PadRight(120).Substring(0, 120)).Trim();
            dwData["EN"] = (Helper.ConvertGB2312To8859P1(str).PadRight(120).Substring(0, 120)).Trim();

            filter = "MLKEY='" + dwData["MLKEY"].ToString() + "'";
            rows = TBPORG.Select(filter);
            if (rows.Length == 0)
            {
                BATCHWB14(dwData);
            }
        }
    }
    public partial class VICC_MULLANG
    {

        public string MANDANT { get { if (dwMaster == null) return ""; return dwMaster["MANDANT"].ToString(); } set { if (dwMaster == null) return; dwMaster["MANDANT"] = value; OnPropertyChanged("MANDANT", value); } } //lENGth 2
        public string ROWNID { get { if (dwMaster == null) return ""; return dwMaster["ROWNID"].ToString(); } set { if (dwMaster == null) return; dwMaster["ROWNID"] = value; OnPropertyChanged("ROWNID", value); } } //lENGth 20
        public string MLKEY { get { if (dwMaster == null) return ""; return dwMaster["MLKEY"].ToString(); } set { if (dwMaster == null) return; dwMaster["MLKEY"] = value; OnPropertyChanged("MLKEY", value); } } //lENGth 40
        public string EN { get { if (dwMaster == null) return ""; return dwMaster["EN"].ToString(); } set { if (dwMaster == null) return; dwMaster["EN"] = value; OnPropertyChanged("EN", value); } } //lENGth 120
        public string SCN { get { if (dwMaster == null) return ""; return dwMaster["SCN"].ToString(); } set { if (dwMaster == null) return; dwMaster["SCN"] = value; OnPropertyChanged("SCN", value); } } //lENGth 120
        public string MLLOC { get { if (dwMaster == null) return ""; return dwMaster["MLLOC"].ToString(); } set { if (dwMaster == null) return; dwMaster["MLLOC"] = value; OnPropertyChanged("MLLOC", value); } } //lENGth 40

    }
}
